package cn.sc.allinpay.allfavour.business.sale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.pai.mvp.callback.MpCallBack;
import cn.pai.mvp.intervenor.Intervenor;
import cn.pai.mvp.presenter.PaiPresenter;
import cn.sc.allinpay.allfavour.R;
import cn.sc.allinpay.allfavour.app.Constant;
import cn.sc.allinpay.allfavour.business.common.data.SaleCouponData;
import cn.sc.allinpay.allfavour.business.saledetail.SaleDetailActivity;
import cn.sc.allinpay.allfavour.business.saletrans.SaleTransActivity;

/**
 * 售券 presenter
 * author：pany
 * on 2017/12/1 11:27
 */
public class SalePresenter extends PaiPresenter<SaleContract.View> implements SaleContract.Presenter {

    public static final int REQ_SALECOUPON_DETAIL = 0x01;

    private SaleModel model = new SaleModel();
    private List<SaleCouponData> saleDatas;

    @Override
    protected Intervenor createIntervenor() {
        return new Intervenor() {
            @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (Activity.RESULT_OK == resultCode) {
                    if (REQ_SALECOUPON_DETAIL == requestCode) {
                        reqQrySaleCoupon();
                    }
                }
            }
        };
    }

    @Override
    public void start() {
        String[] types = view.getContext().getResources().getStringArray(R.array.coupon_type);
        view.setTypeAdapter(Arrays.asList(types));
        reqQrySaleCoupon();
    }

    /**
     * 请求售券列表
     */
    private void reqQrySaleCoupon() {
        view.loading();
        model.reqQrySaleCoupon(new MpCallBack<List<SaleCouponData>, String>() {
            @Override
            public void onSuccess(List<SaleCouponData> resp) {
                view.loaded();
                saleDatas = resp;
                changeSaleAdapterByType();
            }

            @Override
            public void onFailure(String response, String message) {
                view.loaded();
                view.prompt(message);
            }
        });
    }

    /**
     * 筛选数据
     *
     * @return
     */
    private List<SaleCouponData> getFilterList() {
        int postion = view.getType();
        if (0 == postion || 1 == postion) {
            return saleDatas;
        }
        postion -= 2;
        List<SaleCouponData> list = new ArrayList<>();
        for (int i = 0; i < saleDatas.size(); i++) {
            SaleCouponData data = saleDatas.get(i);
            if (postion == data.getAmtType()) {
                list.add(data);
            }
        }
        return list;
    }

    @Override
    public void changeSaleAdapterByType() {
        if (saleDatas == null || saleDatas.isEmpty()) {
            return;
        }
        view.saleAdapterChange(getFilterList());
    }

    @Override
    public void startSaleDetailActivity(SaleCouponData data) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(Constant.BundleKey.SALECOUPON, data);
        view.toActivityForResult(SaleDetailActivity.class, bundle, REQ_SALECOUPON_DETAIL);
    }

    @Override
    public void startSaleTransActivity() {
        view.toActivity(SaleTransActivity.class, null);
    }
}
